//
//  SelectCityView.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/9/20.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "SelectCityView.h"

@interface SelectCityView()<UIPickerViewDataSource, UIPickerViewDelegate>
{
    NSDictionary *_pickerDic;
    NSArray *_provinceArray;
    NSArray *_cityArray;
    NSArray *_townArray;
    NSArray *_selectedArray;
    
    UIView *_alphaView;
    UIView *_showView;
    
    NSString *_provinceStr;
    NSString *_cityStr;
    NSString *_townStr;
}

@property(nonatomic,strong) UIPickerView *pickerView;

@end

@implementation SelectCityView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame] ) {
        
        [self buildView];
        [self getPickerData];
    }
    return self;
}


#pragma mark >>>>>>>>确定取消
- (void)selectCitySureClick:(UIButton *)sender
{
    //省份暂不需要
    _cityStr = [_cityArray objectAtIndex:[_pickerView selectedRowInComponent:1]];
    _townStr = [_townArray objectAtIndex:[_pickerView selectedRowInComponent:2]];
    
    if ([self.delegate respondsToSelector:@selector(SelectCityViewCityStr:townStr:)]) {
        [self.delegate SelectCityViewCityStr:_cityStr townStr:_townStr];
    }
    
    [self dismissPickerView];
}


- (void)selectCityCancleClick:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(SelectCityViewCancle)]) {
        [self.delegate SelectCityViewCancle];
    }
}


- (void)showPickerView
{
    [_showView addSubview:_pickerView];
    
    [UIView animateWithDuration:0.35 animations:^{
        _alphaView.alpha = 0.35;
        _showView.y = HEIGHT/3*2;
    }];
    
    [[self windowView] addSubview:self];
}
/**
 *  消失
 */
- (void)dismissPickerView
{
    [UIView animateWithDuration:0.35 animations:^{
        _alphaView.alpha = 0;
        _showView.y = HEIGHT;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.pickerView removeFromSuperview];
    }];
}

- (UIView *)windowView
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    return window.subviews[0];
}

- (void)getPickerData {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"];
    _pickerDic = [[NSDictionary alloc] initWithContentsOfFile:path];
    _provinceArray = [_pickerDic allKeys];
    _selectedArray = [_pickerDic objectForKey:[[_pickerDic allKeys] objectAtIndex:0]];
    
    if (_selectedArray.count > 0) {
        _cityArray = [[_selectedArray objectAtIndex:0] allKeys];
    }
    
    if (_cityArray.count > 0) {
        _townArray = [[_selectedArray objectAtIndex:0] objectForKey:[_cityArray objectAtIndex:0]];
    }
    
}

#pragma mark >>>>>>>>>>>>> pickerView 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return _provinceArray.count;
    } else if (component == 1) {
        return _cityArray.count;
    } else {
        return _townArray.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        return [_provinceArray objectAtIndex:row];
    } else if (component == 1) {
        return [_cityArray objectAtIndex:row];
    } else {
        return [_townArray objectAtIndex:row];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return WIDTH/3;
    } else if (component == 1) {
        return  WIDTH/3;
    } else {
        return  WIDTH/3;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        _selectedArray = [_pickerDic objectForKey:[_provinceArray objectAtIndex:row]];
        if (_selectedArray.count > 0) {
            _cityArray = [[_selectedArray objectAtIndex:0] allKeys];
        } else {
            _cityArray = nil;
        }
        if (_cityArray.count > 0) {
            _townArray = [[_selectedArray objectAtIndex:0] objectForKey:[_cityArray objectAtIndex:0]];
        } else {
            _townArray = nil;
        }
    }
    [pickerView selectedRowInComponent:1];
    [pickerView reloadComponent:1];
    [pickerView selectedRowInComponent:2];
    
    if (component == 1) {
        if (_selectedArray.count > 0 && _cityArray.count > 0) {
            _townArray = [[_selectedArray objectAtIndex:0] objectForKey:[_cityArray objectAtIndex:row]];
        } else {
            _townArray = nil;
        }
        [pickerView selectRow:1 inComponent:2 animated:YES];
    }
    
    [pickerView reloadComponent:2];
}

- (void)buildView
{

    _showView = [[UIView alloc] initWithFrame:CGRectMake(0,HEIGHT,WIDTH,HEIGHT/3)];
    [self addSubview:_showView];
    _showView.backgroundColor = [UIColor whiteColor];
    
    UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(10,5,40,30)];
    [cancleBtn setBackgroundColor:[UIColor whiteColor]];
    [_showView addSubview:cancleBtn];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn setTitleColor:DEFAULT_COLOR forState:UIControlStateNormal];
    cancleBtn.titleLabel.font = FONT(15);
    [cancleBtn addTarget:self action:@selector(selectCityCancleClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(WIDTH-50,5,40,30)];
    [_showView addSubview:sureBtn];
    [sureBtn setBackgroundColor:[UIColor whiteColor]];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:DEFAULT_COLOR forState:UIControlStateNormal];
    sureBtn.titleLabel.font = FONT(15);
    [sureBtn addTarget:self action:@selector(selectCitySureClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //点击黑色背景 消失
    UIButton *disButton = [UIButton buttonWithType:UIButtonTypeCustom];
    disButton.frame = CGRectMake(0,0,WIDTH,HEIGHT/3*2);
    [self addSubview:disButton];
    [disButton addTarget:self action:@selector(selectCityCancleClick:) forControlEvents:UIControlEventTouchUpInside];

    
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(cancleBtn.frame),WIDTH,HEIGHT/3-40)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.showsSelectionIndicator = YES;
    
    //半透明视图
    _alphaView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,WIDTH,HEIGHT)];
    _alphaView.backgroundColor = [UIColor blackColor];
    _alphaView.alpha = 0;
    [[self windowView] addSubview:_alphaView];
    
    
}


@end
